package ro.msg.quiz.view;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

//AUTHOR: Ervin

//View for adding questions for a new quiz, after it's name was given
@SuppressWarnings("serial")
public class AddNewQuiz2 extends JFrame {
    private JLabel textLabel = new JLabel("Please select the question to add to the questionnaire");
    private JLabel selectLabel = new JLabel("Select");
    private JLabel questionLabel = new JLabel("questions randomly.");
    private JLabel searchLabel = new JLabel("Search your question!");

    private JTextField searchField = new JTextField(12);
    private JTextField field = new JTextField(8);

    private JButton back = new JButton("Back");
    private JButton cancel = new JButton("Cancel");
    private JButton finish = new JButton("Finish");

    private JTable table;
    private DefaultTableModel tableModel = new DefaultTableModel();
    private JScrollPane scrollTable;
    @SuppressWarnings("unused")
    private JCheckBox checkBox = new JCheckBox();

    private JPanel panel = new JPanel();

    public AddNewQuiz2(String[] questions) {

	int frameHeight = questions.length * 40 + 320;

	if (frameHeight < 600)
	    this.setSize(new Dimension(550, frameHeight));
	else
	    this.setSize(new Dimension(550, 600));

	this.setTitle("Smart Questionnaire 1.0 Add new questionnaire");

	panel.setLayout(null);
	textLabel.setBounds(40, 15, 350, 15);
	panel.add(textLabel);

	// /////////////////////////////////////Table////////////////////////////////
	String[] column = { "Add", "Questions" };
	Object[][] data = new Object[questions.length][2];

	int i;
	// //////initialise second row of the table with questions received as
	// parameter///////////
	for (i = 0; i < questions.length; i++) {
	    data[i][1] = questions[i];
	    data[i][0] = false;
	}
	tableModel.setDataVector(data, column);

	// /////////////Anonymous class. Cool Stuff there!////////////////
	table = new JTable(tableModel) {
	    @Override
	    public TableCellRenderer getCellRenderer(int row, int column) {
		if (getValueAt(row, column) instanceof Boolean) {
		    return super.getDefaultRenderer(Boolean.class);
		} else {
		    return super.getCellRenderer(row, column);
		}
	    }

	    @Override
	    public TableCellEditor getCellEditor(int row, int column) {
		if (getValueAt(row, column) instanceof Boolean) {
		    return super.getDefaultEditor(Boolean.class);
		} else {
		    return super.getCellEditor(row, column);
		}
	    }

	    @Override
	    public boolean isCellEditable(int row, int clmn) {
		if (clmn == 0)
		    return true;
		else
		    return false;
	    }
	};
	// /////////////////////////////////////////////////////////////

	table.setRowHeight(40);

	TableColumnModel tcm = table.getColumnModel();
	TableColumn clmn = tcm.getColumn(1);
	clmn.setPreferredWidth(350);

	table.setPreferredScrollableViewportSize(new Dimension(450, 200));
	table.setFillsViewportHeight(true);

	scrollTable = new JScrollPane(table);

	int tableHeightEndPosition = questions.length * 40;

	if (tableHeightEndPosition < 350)
	    scrollTable.setBounds(40, 35, 450, tableHeightEndPosition + 22);
	else {
	    scrollTable.setBounds(40, 35, 450, 375);
	    tableHeightEndPosition = 350;
	}
	panel.add(scrollTable);
	// /////////////////////////////End of
	// table///////////////////////////////////

	selectLabel.setBounds(40, tableHeightEndPosition + 80, 40, 40);
	panel.add(selectLabel);

	field.setBounds(90, tableHeightEndPosition + 90, 80, 20);
	panel.add(field);

	questionLabel.setBounds(180, tableHeightEndPosition + 80, 140, 40);
	panel.add(questionLabel);

	searchLabel.setBounds(40, tableHeightEndPosition + 150, 180, 40);
	panel.add(searchLabel);

	searchField.setBounds(190, tableHeightEndPosition + 160, 180, 20);
	panel.add(searchField);

	back.setBounds(40, tableHeightEndPosition + 220, 80, 25);
	panel.add(back);

	cancel.setBounds(340, tableHeightEndPosition + 220, 80, 25);
	panel.add(cancel);

	finish.setBounds(440, tableHeightEndPosition + 220, 80, 25);
	panel.add(finish);

	this.add(panel);

	this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	this.setVisible(true);
    }

    @SuppressWarnings("rawtypes")
    public List<Integer> getSelectedQuestions() {
	Vector data = tableModel.getDataVector();
	List<Integer> selection = new ArrayList<Integer>();
	for (int i = 0; i < data.size(); i++)
	    if (((Vector) data.elementAt(i)).elementAt(0).equals(true)) {
		selection.add(i);
	    }

	return selection;
    }

    public void searchFilterTable(String s, String[] questions) {
	if (s != null) {
	    String[] column = { "Add", "Questions" };
	    // Vector data = tableModel.getDataVector();
	    List<String> strings = new ArrayList<String>();

	    for (int i = 0; i < questions.length; i++)
		if (questions[i].toLowerCase().contains(s.toLowerCase()))
		    strings.add(questions[i]);

	    Object[][] data2 = new Object[strings.size()][2];
	    for (int i = 0; i < strings.size(); i++) {
		data2[i][0] = false;
		data2[i][1] = strings.get(i);
	    }

	    tableModel.setDataVector(data2, column);

	    TableColumnModel tcm = table.getColumnModel();
	    TableColumn clmn = tcm.getColumn(1);
	    clmn.setPreferredWidth(350);
	}

    }

    public JButton getBack() {
	return back;
    }

    public JButton getCancel() {
	return cancel;
    }

    public JButton getFinish() {
	return finish;
    }

    public JTextField getSearchField() {
	return searchField;
    }

    public String getSearchText() {
	return searchField.getText();
    }

    public void closeFrame() {
	this.dispose();
    }

}
